<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class MemberStar extends Basic
{
    
    public static function tableName()
    {
        return '{{%member_star}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'required', 'message' => 'member_id不能为空', 'on' => 'add'],
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'member_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'member_id最大存储18446744073709551615'],
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['sflag', 'integer', 'message' => 'sflag必须为数字，或数字字符串'],
            ['sflag', 'in', 'range' => array(1, 2, 3), 'message' => 'sflag值必须在1~3中选择'], 
            ['cflag', 'integer', 'message' => 'cflag必须为数字，或数字字符串'],
            ['cflag', 'in', 'range' => array(1, 2), 'message' => 'cflag值必须在1~2中选择'], 
            ['contentid', 'required', 'message' => 'contentid不能为空', 'on' => 'add'],
            ['contentid', 'integer', 'message' => 'contentid必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'contentid最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'contentid最大存储18446744073709551615'],
            ['nickname', 'string', 'message' => 'nickname必须是字符串',
                                'max' => 18, 'tooLong' => 'nickname最长18位'],
            ['headimgurl', 'string', 'message' => 'headimgurl必须是字符串',
                                'max' => 512, 'tooLong' => 'headimgurl最长512位'],
            ['comments', 'default', 'value' => ''],
            ['comments', 'string', 'message' => 'comments必须是字符串'],      
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addStar($data)
    {
        // P($data);
        $data['MemberStar']['add_time'] = date('Y-m-d H:i:s', time());
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*关联查询 内容*/
    public function getContent()
    {
        $res = $this->hasOne(Content::className(), ['content_id' => 'contentid'])
                    ->select(['content_id', 'title', 'preview', 'describe', 'star_num', 'praise_num', 'reply_num']);
        return $res;
    }

    
    /*关联查询 动态数据*/
    public function getDynamicInfo()
    {
        $res = $this->hasOne(DynamicDataInfo::className(), ['info_id' => 'contentid'])
                    ->select(['info_id', 'star_num', 'praise_num', 'reply_num']);
        return $res;
    }

    /*关联查询 动态数据*/
    public function getDynamic()
    {
        $res = $this->hasMany(DynamicDataList::className(), ['info_id' => 'contentid']);
        return $res;
    }

    
}